package org.vartist.atld.menu.activity.util.base64;

import org.vartist.atld.R;
import org.vartist.atld.menu.consts.LayoutConst;
import org.vartist.atld.menu.task.util.base64.ConvertBase64ToImageTask;
import org.vartist.atld.menu.task.util.base64.ConvertImageToBase64Task;
import org.vartist.atld.menu.task.util.base64.LoadImageTask;
import org.vartist.atld.util.image.ImageUtil;
import org.vartist.atld.util.log.LogUtil;
import org.vartist.atld.util.ui.TitleUtil;
import org.vartist.atld.view.loading.Loading;
import org.vartist.atld.view.loading.LoadingCallback;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Base64Activity extends Activity implements LoadingCallback {

	private static final String IMAGE_URL = "http://www.google.co.kr/images/srpr/logo3w.png";
	
	private Loading mLoading = null;
	private Bitmap mResultBitmapImage = null; 
	private boolean mIsLoadImage = false;
	private String mImageURL = null;
	private String mEncodedText = null;
	private LoadImageTask mLoadImageTask = null;
	private ConvertImageToBase64Task mConvertToBase64Task = null;
	private ConvertBase64ToImageTask mConvertToImageTask = null;
	
	private EditText mTxtImageUrl = null;
	private TextView mTxtBase64Result = null;
	private Button mBtnLoadImage = null;
	private Button mBtnConvertToBase64 = null;
	private Button mBtnConvertToBitmap = null;
	private ImageView mImgLoadImageResult = null;
	private ImageView mImgReverseConvertringImageResult = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogUtil.debugNativeHeap();
		
		TitleUtil.setContentView(this, LayoutConst.LAYOUT_FOR_BASE64, getString(R.string.app_name), getString(R.string.menu_util_base64_name));
		getLayouts();
		
		mImageURL = getImageURL();
		mBtnLoadImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startLoadImageTask();
			}
		});
	}

	@Override
	public void onBackkeyDown() {
		finishAllTask();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finishAllTask();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		finishAllTask();		
	}

	private void finishAllTask() {
		stopLoadImageTask();
		stopConvertToBase64Task();
		stopConvertToImageTask();
	}
	
	private String getImageURL() {
		return mTxtImageUrl.getText().toString();
	}

	private void startLoadImageTask() {
		stopLoadImageTask();
		
		if (mImageURL != null && mImageURL.length() > 0) {
			clearResultImageView();
			
			mLoadImageTask = new LoadImageTask(Base64Activity.this, mImageURL);	
			mLoadImageTask.execute();
		} else {
			LogUtil.e(getString(R.string.error_parameter_not_enough));
		}
	}
	
	private void stopLoadImageTask() {
		if (mLoadImageTask != null && !mLoadImageTask.isCancelled()) {
			mLoadImageTask.cancel(true);
			mLoadImageTask = null;
		}
		
		setProgressVisibility(false);
	}

	private void startConvertToBase64Task() {
		stopConvertToBase64Task();
		
		if (mResultBitmapImage != null) {
			clearResultBase64TextView();
			
			mConvertToBase64Task = new ConvertImageToBase64Task(Base64Activity.this, mResultBitmapImage);
			if (mConvertToBase64Task.getStatus() != AsyncTask.Status.RUNNING) {
				mConvertToBase64Task.execute();			
			}
		}
	}
	
	private void stopConvertToBase64Task() {
		if (mConvertToBase64Task != null && !mConvertToBase64Task.isCancelled()) {
			mConvertToBase64Task.cancel(true);
			mConvertToBase64Task = null;
		}
		
		setProgressVisibility(false);
	}
	
	private void startConvertToImageTask() {
		stopConvertToImageTask();
		
		if (mEncodedText != null && mEncodedText.length() > 0) {
			clearResultReverseConvertImageView();
			
			mConvertToImageTask = new ConvertBase64ToImageTask(Base64Activity.this, mEncodedText);
			if (mConvertToImageTask.getStatus() != AsyncTask.Status.RUNNING) {
				mConvertToImageTask.execute();
			}
		}
		
	}
	
	private void stopConvertToImageTask() {
		if (mConvertToImageTask != null && !mConvertToImageTask.isCancelled()) {
			mConvertToImageTask.cancel(true);
			mConvertToImageTask = null;
		}
		
		setProgressVisibility(false);
	}

	private void activationConvertButton() {
		if (mIsLoadImage) {
			mBtnConvertToBase64.setEnabled(true);
			
			if (mResultBitmapImage != null) {
				mBtnConvertToBase64.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startConvertToBase64Task();
					}
				});
			}
		}
	}
	
	public void clearResultImageView() {
		ImageUtil.recycleAfterCheck(mResultBitmapImage);
		mImgLoadImageResult.setImageBitmap(null);
		mImgLoadImageResult.setImageDrawable(getResources().getDrawable(R.drawable.sample_default_thumnail));
	}

	public void clearResultBase64TextView() {
		if (mTxtBase64Result != null) {
			if (mTxtBase64Result.getText().length() > 0 ) {
				mTxtBase64Result.setText("");
			}
		}
	}

	public void clearResultReverseConvertImageView() {
		if (mImgReverseConvertringImageResult != null) {
			mImgReverseConvertringImageResult.setImageBitmap(null);
			mImgReverseConvertringImageResult.setImageDrawable(getResources().getDrawable(R.drawable.sample_default_thumnail));
		}
	}

	public void setResultForLoadImage(Bitmap bitmapImage) {
		if (bitmapImage != null) {
			mImgLoadImageResult.setImageBitmap(bitmapImage);
			mIsLoadImage = true;
			
			mResultBitmapImage = bitmapImage;
			
			activationConvertButton();

		} else {
			LogUtil.e("bitmap object from by loading image task " + getString(R.string.error_msg_null));
		}
	}
	
	public void setResultForConvertBase64(String encodedText) {
		mEncodedText = encodedText;
		mTxtBase64Result.setText(mEncodedText);
		mTxtBase64Result.setMovementMethod(new ScrollingMovementMethod());
		
		mBtnConvertToBitmap.setEnabled(true);
		mBtnConvertToBitmap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startConvertToImageTask();
			}
		});
	}

	public void setReverseConvertedBitmap(Bitmap bitmap) {
		mImgReverseConvertringImageResult.setImageBitmap(bitmap);
	}
	
	public void setProgressVisibility(boolean visible) {
		if (mLoading != null) {
			if (visible) {
				if (!mLoading.isShowing()) {
					mLoading.show();					
				}
			}else{
				if (mLoading.isShowing()) {
					mLoading.dismiss();					
				}
			}			
		}
	}
	
	private void getLayouts() {
		mLoading = new Loading(this, this);
		
		mTxtImageUrl = (EditText) findViewById(R.id.txtImageUrl);
		mTxtImageUrl.setText(IMAGE_URL);
		
		mBtnLoadImage = (Button) findViewById(R.id.btnLoadImage);
		mBtnConvertToBase64 = (Button) findViewById(R.id.btnConvertToBase64);
		mImgLoadImageResult = (ImageView) findViewById(R.id.imgLoadImageResult);
		mImgReverseConvertringImageResult = (ImageView) findViewById(R.id.imgReverseConvertringImageResult);
		
		mTxtBase64Result = (TextView) findViewById(R.id.txtBase64Result);
		mBtnConvertToBitmap = (Button) findViewById(R.id.btnConvertToBitmap);
	}
}